﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace caufingHealth.Forms
{
    public partial class SearchResults : Form
    {
        public int SelectedID
        {
            get;
            private set;
        }
        public SearchResults()
        {
            InitializeComponent();
        }
        
        public SearchResults(List<KeyValuePair<string, object>>[] results) : this()
        {
            cb_Records.ValueMember = "Value";
            cb_Records.DisplayMember = "Name";

            foreach (List<KeyValuePair<string, object>> record in results)
            {
                string firstName = null;
                string lastName = null;
                DateTime dob = new DateTime();
                int id = 0;

                foreach (KeyValuePair<string, object> pair in record)
                {
                    if (pair.Key.Equals("FirstName"))
                        firstName = (string)pair.Value;
                    else if (pair.Key.Equals("LastName"))
                        lastName = (string)pair.Value;
                    else if (pair.Key.Equals("id"))
                        id = (int)pair.Value;
                    else if (pair.Key.Equals("DateOfBirth"))
                        dob = new DateTime((long)pair.Value);
                }

                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Name = string.Format("Name: {0} {1} - DOB: {2}", firstName, lastName, dob.ToShortDateString());
                cbi.Value = id;

                cb_Records.Items.Add(cbi);
            }
            cb_Records.SelectedIndex = 0;
        }

        public SearchResults(PatientInformation.PatientRecord[] results)
            : this()
        {
            cb_Records.ValueMember = "Value";
            cb_Records.DisplayMember = "Name";

            foreach (PatientInformation.PatientRecord record in results)
            {
                string firstName = record.Name.FirstName;
                string lastName = record.Name.LastName;
                String[] dob = new String[]{ record.DateOfBirth.Month, record.DateOfBirth.Day, record.DateOfBirth.Year};
               // DateTime dob = record.DateOfBirth.;
                int id = 0;

                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Name = string.Format("Name: {0} {1} - DOB: {2}/{3}/{4}", firstName, lastName, dob[0],dob[1],dob[2]);
                cbi.Value = id++;

                cb_Records.Items.Add(cbi);
            }
            cb_Records.SelectedIndex = 0;
        }
        private class ComboBoxItem
        {
            public string Name { get; set; }
            public int Value { get; set; }
        }

        private void btn_Select_Click(object sender, EventArgs e)
        {

            SelectedID = ((cb_Records.SelectedItem as ComboBoxItem).Value); 
        }
    }
}
